﻿using System;
using Framework.Library.Base.Base;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.Security;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;

namespace Framework.Portal.Layouts.Admin.Components.Users
{
    public partial class UserPassword : BaseControl
    {
        private int userId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblUpdateStatus.Text = string.Empty;
                userId = ConvertUtility.ToInt32(Request.QueryString["uid"]);
                if (userId > 0)
                {
                    PortalUser user = ManagerFactory.PortalUserManager.GetUser(userId);
                    if (user != null)
                    {
                        lblUsername.Text = user.USERNAME;
                        hddPassword.Value = user.PASSWORD;
                    }
                }
                else
                    Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
            }
        }

        protected void btnManagePassword_Click(object sender, EventArgs e)
        {
            string curPassword = Crypto.MD5Encrypt(txtPassword.Text.Trim());
            if (curPassword == hddPassword.Value)
            {
                ManagerFactory.DAOEntity.Update(new PortalUser() { USERID = userId, PASSWORD = curPassword });
                Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
            }
            else
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_ERROR");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
        }
    }
}